package model

import (
	com_model "elflog/common/com-model"
	"encoding/json"
)

type CreateAccountLoggerModel struct {
	AccountId         string `json:"account_id"`
	Name              string `json:"name"`
	MaxSaveCount      int    `json:"max_save_count"`
	MaxSaveDuration   int    `json:"max_save_duration"`
	FieldNameStrategy string `json:"field_name_strategy" gorm:"column:field_name_strategy;not null"` // {"trace_id":"traceId"}...
	Key               string `json:"key"`
	Secret            string `json:"secret"`
}

type FieldNameStrategy map[string]string

var defaultFieldNameStrategy FieldNameStrategy = FieldNameStrategy{
	com_model.AppName:    com_model.AppName,
	com_model.AppVersion: com_model.AppVersion,
	com_model.AppHost:    com_model.AppHost,
	com_model.RemoteIp:   com_model.RemoteIp,
	com_model.ModuleName: com_model.ModuleName,
	com_model.FuncName:   com_model.FuncName,
	com_model.ClientId:   com_model.ClientId,
	com_model.Time:       com_model.Time,
	com_model.TraceId:    com_model.TraceId,
	com_model.UserId:     com_model.UserId,
	com_model.Message:    com_model.Message,
	com_model.Level:      com_model.Level,
}

func NewFieldNameStrategy(strategyStr string) FieldNameStrategy {
	strategy := make(FieldNameStrategy, 0)
	err := json.Unmarshal([]byte(strategyStr), &strategy)
	if err != nil {
		return defaultFieldNameStrategy
	} else {
		return strategy
	}
}

func (f *FieldNameStrategy) HasFieldName(name string) bool {
	if f == nil {
		return false
	}
	_, ok := (*f)[name]
	return ok
}

func (f *FieldNameStrategy) GetOriginFieldName(name string) string {
	if f == nil {
		return name
	}
	val, ok := (*f)[name]
	if ok {
		return val
	} else {
		return name
	}
}
